package com.ti.ouxun.ui;

import com.ti.ouxun.util.ExitProgramUtil;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

/**
 * 
 * @author kTian
 *
 * 2013-12-31下午2:47:51
 */
public class MainActivity extends FragmentTabHostActivity {
	
	
    public void addFragment(FragmentManager fm, Class<? extends Fragment> fragmentClass, String tag, Bundle args) {
    	addFragmentAnim(fm, fragmentClass, tag, args, 
				new int[]{android.R.anim.fade_in, android.R.anim.fade_out, 
								android.R.anim.fade_in, android.R.anim.fade_out});
    }
    
    public void addFragment(FragmentManager fm, Fragment fragment, String tag, Bundle args) {
    	addFragmentAnim(fm, fragment, tag, args, 
				new int[]{android.R.anim.fade_in, android.R.anim.fade_out, 
								android.R.anim.fade_in, android.R.anim.fade_out});
	}
    
    public void addFragmentAnim(FragmentManager fm, Fragment fragment, String tag, Bundle args, int[] anims) {
		baseFragmentOperate(fm, fragment, R.id.fragment_container, tag, args, anims, true, true);
	}

	public void addFragmentAnim(FragmentManager fm, Class<? extends Fragment> fragmentClass, String tag, Bundle args, int[] anims) {
		baseFragmentOperate(fm, fragmentClass, R.id.fragment_container, tag, args, anims, true, true);
	}
	
	/**
	 * @param fm
	 * @param fragmentClass
	 * @param fragmentContainerId
	 * @param tag
	 * @param args
	 * @param anims	切换过渡动画数组，如果数组长度为 2 那么就是 enterAnim, exitAnim,  如果为 4 就是 enterAnim, exitAnim, popEnterAnim, popExitAnim
	 * @param isAdd 是否添加 该 Fragment, true 添加（add）， false 替换（replace）
	 */
	public void baseFragmentOperate(FragmentManager fm, Class<? extends Fragment> fragmentClass, int fragmentContainerId, String tag, Bundle args,int[] anims, boolean isAdd, boolean isAddBackStack) {
		Fragment fragment = getFragmentInstance(fm, fragmentClass, tag, args);
		baseFragmentOperate(fm, fragment, fragmentContainerId, tag, args, anims, isAdd, isAddBackStack);
	}
	
	public void baseFragmentOperate(FragmentManager fm, Fragment fragment, int fragmentContainerId, String tag, Bundle args, int[] anims, boolean isAdd, boolean isAddBackStack) {
		if( args != null ) {
			Bundle oldArgs = fragment.getArguments();
			if( oldArgs != null ) {
				oldArgs.putAll( args );
			} else {
				fragment.setArguments(args);
			}
		}
		FragmentTransaction ft = fm.beginTransaction();
		if( fragment.isAdded() ) {
			ft.remove(fragment);
			ft.commit();
			ft = fm.beginTransaction();
		}
		setCustomAnim(ft, anims);
		if( isAdd ) {
			ft.add(fragmentContainerId, fragment, tag);
		} else {
			ft.replace(fragmentContainerId, fragment, tag);
		}
		if( isAddBackStack ) {
			ft.addToBackStack(tag);
		}
		ft.commitAllowingStateLoss();
	}
	
	public Fragment getFragmentInstance(FragmentManager fm, Class<? extends Fragment> fragmentClass, String tag, Bundle args) {
		Fragment fragment = fm.findFragmentByTag(tag);
		if( fragment == null ) {
			fragment = createNewFragment(fm, fragmentClass);
		}
		
		if( args != null && !args.isEmpty() ) {
			fragment.getArguments().putAll(args);
		}
		return fragment;
	}
	
	public Fragment createNewFragment(FragmentManager fm, Class<? extends Fragment> fragmentClass) {
		Fragment fragment = null;
		try {
			fragment = fragmentClass.newInstance();
			fragment.setArguments(new Bundle());
		} catch (java.lang.InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return fragment;
	}
	
	/**
	 * 
	 * @param ft
	 * @param anims	切换过渡动画数组，如果数组长度为 2 那么就是 enterAnim, exitAnim,  如果为 4 就是 enterAnim, exitAnim, popEnterAnim, popExitAnim
	 */
	private void setCustomAnim(FragmentTransaction ft, int[] anims) {
		if( anims == null ) {
			return;
		}
		int animCount = anims.length;
		if( animCount == 2 ) { 
			ft.setCustomAnimations( anims[0], anims[1] );
		} else if( animCount == 4 ) {
			ft.setCustomAnimations( anims[0], anims[1], anims[2], anims[3] );
		}
	}
	
	public void popFragmentBackStack() {
		getSupportFragmentManager().popBackStack();
	}

	@Override
	public void onBackPressed() {
		// 连续点击两次退出程序
		ExitProgramUtil.exit(MainActivity.this);
	}
    
}
